package cn.ekuma.data.ui.android;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;

import cn.ekuma.android.Animation.ExpandCollapseAnimation;
import cn.ekuma.android.ui.filter.BaseFilterPanel;

/**
 * Created by Administrator on 2014/12/11.
 */
public class BeanFindPanel  extends LinearLayout implements View.OnTouchListener {
    LinearLayout titlePanel;
    FrameLayout contentLayout;
    private ImageButton zoomImgBtn;
    protected BaseFilterPanel filterPanel;
    protected BaseBeanListSelecter main;
    ListView listView;

    private boolean waitDouble = true;

    private static final int DOUBLE_CLICK_TIME = 350;

    public BeanFindPanel(Context context,BaseFilterPanel.FilterPanelAdapter adapter,BaseAdapter listAdapter) {
        super(context);
        init(context,adapter,listAdapter);
    }

    private void init(final Context context,BaseFilterPanel.FilterPanelAdapter adapter,BaseAdapter listAdapter) {
        View mainView = LayoutInflater.from(context).inflate(R.layout.abstract_view, this);

        titlePanel=(LinearLayout)findViewById(R.id.titlePanel);

        filterPanel=new BaseFilterPanel(context,adapter);

        titlePanel.addView(filterPanel);
        contentLayout = (FrameLayout) findViewById(R.id.contentPanel);

        View c = createContentPanel( listAdapter);
        if (c != null){
            FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            contentLayout.addView(c,lp1);
            zoomImgBtn=new ImageButton(context);
            zoomImgBtn.setImageResource(R.drawable.tree_collapse);
            zoomImgBtn.setOnClickListener(listener);

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //此处相当于布局文件中的Android:layout_gravity属性
            lp.gravity = Gravity.BOTTOM|Gravity.RIGHT;
            contentLayout.addView(zoomImgBtn,lp);
        }
    }

    OnClickListener listener = new OnClickListener(){
        public void onClick(View v) {
            if(waitDouble == true){
                waitDouble = false;
                Thread thread = new Thread(){
                    @Override
                    public void run(){
                        try {
                            sleep(DOUBLE_CLICK_TIME);
                            if(waitDouble == false){
                                waitDouble = true;
                                singleClick();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread.start();
            }else{
                waitDouble = true;
                doubleClick();
            }
        }
    };

    private void singleClick(){
    }

    private void doubleClick(){
        titlePanel.setVisibility(titlePanel.getVisibility()==View.GONE?View.VISIBLE:View.GONE);
        zoomImgBtn.setImageResource(titlePanel.getVisibility()==View.GONE?R.drawable.tree_expand:R.drawable.tree_collapse);
    }

    public View createContentPanel(BaseAdapter listAdapter) {
        touchSlop = ViewConfiguration.get(this.getContext()).getScaledTouchSlop();
        main=new BaseBeanListSelecter(getContext());
        main.setAbstractBaseBeanAdapter(listAdapter);
        listView=main.getListview();
        listView.setOnTouchListener(this);
        return main;
    }

    public void setOnBaseBeanSelectedListener(OnBaseBeanSelectedListener l){
        main.setOnBaseBeanSelectedListener(l);
    }

    /**
     * 当ListView被触摸时调用，其中处理了各种下拉刷新的具体逻辑。
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        setIsAbleToPull(event);
        if (ableToPull) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    yDown = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    float yMove = event.getRawY();
                    int distance = (int) (yMove - yDown);
                    // 如果手指是下滑状态，并且下拉头是完全隐藏的，就屏蔽下拉事件

                    if (distance <= 0 &&distance < touchSlop*4) {
                        return false;
                    }

                    // 通过偏移下拉头的topMargin值，来实现下拉效果
                    //headerLayoutParams.topMargin = (distance / 2) + hideHeaderHeight;
                    //filterPanel.setLayoutParams(headerLayoutParams);
                    if(titlePanel.getVisibility()==View.GONE)
                        //titlePanel.setVisibility(View.VISIBLE);
                        titlePanel.startAnimation(new ExpandCollapseAnimation(titlePanel,ExpandCollapseAnimation.EXPAND));
                    else
                        //titlePanel.setVisibility(View.GONE);
                        titlePanel.startAnimation(new ExpandCollapseAnimation(titlePanel,ExpandCollapseAnimation.COLLAPSE));
                    return true;

            }
        }
        return false;
    }

    /**
     * 根据当前ListView的滚动状态来设定 {@link #ableToPull}
     * 的值，每次都需要在onTouch中第一个执行，这样可以判断出当前应该是滚动ListView，还是应该进行下拉。
     *
     * @param event
     */
    private void setIsAbleToPull(MotionEvent event) {
        View firstChild = listView.getChildAt(0);
        if (firstChild != null) {
            int firstVisiblePos = listView.getFirstVisiblePosition();
            if (firstVisiblePos == 0 && firstChild.getTop() == 0) {
                if (!ableToPull) {
                    yDown = event.getRawY();
                }
                // 如果首个元素的上边缘，距离父布局值为0，就说明ListView滚动到了最顶部，此时应该允许下拉刷新
                ableToPull = true;
            }else
                ableToPull = false;
        } else {
            // 如果ListView中没有元素，不应该允许下拉刷新
            ableToPull = true;
        }
    }


    /**
     * 当前是否可以下拉，只有ListView滚动到头的时候才允许下拉
     */
    private boolean ableToPull;

    /**
     * 手指按下时的屏幕纵坐标
     */
    private float yDown;

    /**
     * 在被判定为滚动之前用户手指可以移动的最大值。
     */
    private int touchSlop;


    /**
     * 下拉头的布局参数
     */
    private ViewGroup.MarginLayoutParams headerLayoutParams;

    /**
     * 下拉头的高度
     */
    private int hideHeaderHeight;
}
